#!/bin/sh
uci -q batch <<-EOF
    delete ucitrack.@openmptcprouter[-1]
    add ucitrack openmptcprouter
    set ucitrack.@openmptcprouter[-1].init=openmptcprouter
    commit ucitrack
EOF
if [ "$(uci -q get qos.serverin)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		set qos.serverin=classify
		set qos.serverin.target='Priority'
		set qos.serverout=classify
		set qos.serverout.target='Priority'
		commit qos
	EOF
fi
if [ "$(uci -q get qos.serverin.target)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		set qos.serverin.target='Priority'
		set qos.serverout.target='Priority'
		commit qos
	EOF
fi
if [ "$(uci -q get ucitrack.@network[-1].affects | grep openmptcprouter)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		add_list ucitrack.@network[-1].affects="openmptcprouter"
		commit ucitrack
	EOF
fi
if [ "$(uci -q show openmptcprouter | grep server)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		set openmptcprouter.vps=server
		set openmptcprouter.vps.username="openmptcprouter"
		commit openmptcprouter
	EOF
fi
if [ "$(uci -q get openmptcprouter.vps)" = "server" ] && [ "$(uci -q get openmptcprouter.vps.master)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		set openmptcprouter.vps.master=1
		set openmptcprouter.vps.backup=0
		commit openmptcprouter
	EOF
fi
if [ "$(uci -q get openmptcprouter.omr)" != "router" ]; then
	uci -q batch <<-EOF >/dev/null
		set openmptcprouter.omr=router
		commit openmptcprouter
	EOF
fi
if [ "$(uci -q get openmptcprouter.settings.master)" = "dynamic" ]; then
	uci -q batch <<-EOF >/dev/null
		set openmptcprouter.settings.master='change'
		commit openmptcprouter
	EOF
fi
if [ "$(uci -q get openmptcprouter.settings.master)" = "redundant" ] || [ "$(uci -q get openmptcprouter.settings.master)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		set openmptcprouter.settings.master='balancing'
		commit openmptcprouter
	EOF
fi
if [ "$(uci -q get openmptcprouter.vps.port)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		set openmptcprouter.vps.port='65500'
		commit openmptcprouter
	EOF
fi
if [ "$(uci -q get openmptcprouter.settings.disable_ipv6)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		set openmptcprouter.settings.disable_ipv6='1'
		commit openmptcprouter
	EOF
fi
if [ "$(uci -q get openmptcprouter.settings.check_ipv4_website)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		set openmptcprouter.settings.check_ipv4_website='http://ip.openmptcprouter.com'
		commit openmptcprouter
	EOF
fi
if [ "$(uci -q get openmptcprouter.settings.check_ipv6_website)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		set openmptcprouter.settings.check_ipv6_website='http://ipv6.openmptcprouter.com'
		commit openmptcprouter
	EOF
fi
if [ "$(uci -q get openmptcprouter.settings.status_vps_timeout)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		set openmptcprouter.settings.status_vps_timeout=2
		commit openmptcprouter
	EOF
fi
if [ "$(uci -q get openmptcprouter.settings.status_getip_timeout)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		set openmptcprouter.settings.status_getip_timeout=2
		commit openmptcprouter
	EOF
fi
if [ "$(uci -q get openmptcprouter.settings.enable_nodelay)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		set openmptcprouter.settings.enable_nodelay=1
		commit openmptcprouter
	EOF
fi
if [ "$(uci -q get openmptcprouter.settings.scaling_governor)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		set openmptcprouter.settings.scaling_governor='performance'
		commit openmptcprouter
	EOF
fi
if [ "$(uci -q get openmptcprouter.settings.menu)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		set openmptcprouter.settings.menu='OpenMPTCProuter'
		commit openmptcprouter
	EOF
fi

sed -i 's/net.ipv4.tcp_retries2=3$/net.ipv4.tcp_retries2=15/' /etc/sysctl.d/zzz_openmptcprouter.conf

exit 0
